package com.ruoyi.web.controller.hh;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.HhSize;
import com.ruoyi.domain.HhType;
import com.ruoyi.service.IHhSizeService;
import com.ruoyi.service.IHhTypeService;
import com.ruoyi.utils.SelectHelpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品规格Controller
 * @author chongjin
 * @date 2020-12-17
 */
@Controller
@RequestMapping("/baseInfo/size")
public class HhSizeController extends BaseController {
    private String prefix = "hh/baseInfo";

    @Autowired
    private IHhSizeService hhSizeService;
    @Autowired
    private IHhTypeService hhTypeService;

    @GetMapping()
    public String size(ModelMap mmap) {
        HhType hhType = new HhType();
        hhType.setParentId(Long.parseLong("0"));
        List typeList = hhTypeService.selectHhTypeList(hhType);
        mmap.put("parentType", SelectHelpUtil.getNameType(typeList));
        return prefix + "/size_manage";
    }

    /**
     * 查询商品规格列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HhSize hhSize) {
        startPage();
        List<HhSize> list = hhSizeService.selectHhSizeList(hhSize);
        return getDataTable(list);
    }

    /**
     * 导出商品规格列表
     */
    @Log(title = "商品规格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HhSize hhSize) {
        List<HhSize> list = hhSizeService.selectHhSizeList(hhSize);
        ExcelUtil<HhSize> util = new ExcelUtil<HhSize>(HhSize.class);
        return util.exportExcel(list, "size");
    }

    /**
     * 新增商品规格
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        HhType hhType = new HhType();
        hhType.setParentId(Long.parseLong("0"));
        List typeList = hhTypeService.selectHhTypeList(hhType);
        mmap.put("parentType", SelectHelpUtil.getNameType(typeList));
        return prefix + "/size_add";
    }

    /**
     * 新增保存商品规格
     */
    @Log(title = "商品规格", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HhSize hhSize) {
        return toAjax(hhSizeService.insertHhSize(hhSize));
    }

    /**
     * 修改商品规格
     */
    @GetMapping("/edit/{sizeId}")
    public String edit(@PathVariable("sizeId") Long sizeId, ModelMap mmap) {
        HhSize hhSize = hhSizeService.selectHhSizeById(sizeId);
        mmap.put("hhSize", hhSize);
        return prefix + "/size_edit";
    }

    /**
     * 修改保存商品规格
     */
    @Log(title = "商品规格", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HhSize hhSize) {
        return toAjax(hhSizeService.updateHhSize(hhSize));
    }

    /**
     * 删除商品规格
     */
    @Log(title = "商品规格", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(hhSizeService.deleteHhSizeByIds(ids));
    }
}
